package com.caiwangdai.app.ui.verify

import android.content.Context
import android.content.Intent
import android.hardware.Camera
import com.caiwangdai.app.R
import com.caiwangdai.app.backend.RetrofitHome
import com.caiwangdai.app.backend.observer.OnDataV2Listener
import com.caiwangdai.app.backend.rx.RxSchedulers
import com.caiwangdai.app.bean.base.BaseB
import com.caiwangdai.app.bean.base.NormalResponseB
import com.caiwangdai.app.extension.*
import com.caiwangdai.app.origin.BaseActivity
import com.caiwangdai.app.origin.ToolBarManager
import com.caiwangdai.app.widget.CameraView
import kotlinx.android.synthetic.main.activity_face_camera.*
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class FaceCameraActivity : BaseActivity() {
    override fun getLayoutResID() = R.layout.activity_face_camera

    companion object {
        fun newInstance(context: Context): Intent {
            val intent = Intent(context, FaceCameraActivity::class.java)
            return intent
        }
    }


    var currentFile: File? = null //当前拍照的途径
    var confirming = false;


    override fun initView() {
        ToolBarManager.with(this, WMContent)
                .setBackgroundColor(R.color.white)
                .setNavigationIcon(R.drawable.ic_nav_close_circle)
                .setTitle("")


        cameraStatus()

        camera_face.currentCamera = CameraView.CAMERA_FRONT
        camera_face.cameraType = CameraView.CAMERA_TYPE_FACE
        camera_face.setHeight((getScreenWidth() * (4f / 3f)).toInt())

        camera_face.onCameraStatusListener = object : CameraView.OnCameraStatusListener {
            override fun onAutoFocus(success: Boolean) {}

            override fun onTouchFocus(mCamera: Camera) {}

            override fun onCameraStopped(url: String) {
                confirmStatus()
                currentFile = File(url)

            }
        }
        rela_square.setHeight(getScreenWidth())
        //拍照
        btn_snap.setOnClickListenerPro {
            camera_face.takePicture()
        }

        //确定
        btn_confirm.setOnClickListenerPro {
            updateImageFile(currentFile!!)
        }
        //取消
        btn_cancel.setOnClickListenerPro {
            currentFile?.delete()
            camera_face.onResume()
            cameraStatus()
        }
//        ProgressDialogWM.showProgressDialog(this,"提示","相机启动中...")
//        Handler().postDelayed({
//            ProgressDialogWM.hideProgressDialog()
//        }, 700)
    }


    //type=0为正面 type=1为背面
    fun updateImageFile(imageFile: File) {
        //传入multipartbody
        val image = MultipartBody.Part.createFormData("file", imageFile.getName(), RequestBody.create(MultipartBody.FORM, imageFile))
        val uid = MultipartBody.Part.createFormData("uid", getUser()?.id+"")
        val type = MultipartBody.Part.createFormData("type", "face")

        RetrofitHome.getUserApi()
                .postIdCard(image, uid, type)
                .compose(RxSchedulers.toMain())
                .subscribe(object : OnDataV2Listener<BaseB>(this) {
                    override fun getProgressBarContent() = "上传图片中..."

                    override fun onSuccess(value: NormalResponseB<BaseB>) {
                        //delete after used
                        imageFile.delete()

                        startActivity(RealNameVerifyActivity.newInstance(mContext))
                        finish()
                    }
                })
    }


    override fun onResume() {
        super.onResume()
        if (confirming) return
        camera_face.post {
            camera_face.onResume()
        }
    }

    override fun onPause() {
        super.onPause()
        camera_face.onPause()
    }

    override fun onBackPressed() {
        super.onBackPressed()
        currentFile?.delete()
    }


    fun confirmStatus() {
        confirming = true
        btn_confirm.visible()
        btn_cancel.visible()
        btn_snap.invisible()
    }

    fun cameraStatus() {
        confirming = false
        btn_confirm.gone()
        btn_cancel.gone()
        btn_snap.visible()
    }

}
